package com.hngy.xpq.appthemedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class QhThemeActivity extends AppCompatActivity implements View.OnClickListener {

    Button blue, red, yellow, mr;
    SharedPreferences init;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        init();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qh_theme);
        onclick();
    }

    private void onclick() {
        blue = findViewById(R.id.blue);
        red = findViewById(R.id.red);
        yellow = findViewById(R.id.yellow);
        mr = findViewById(R.id.mr);
        blue.setOnClickListener(this);
        red.setOnClickListener(this);
        yellow.setOnClickListener(this);
        mr.setOnClickListener(this);
    }

    private void init() {
        init = super.getSharedPreferences("init", MODE_PRIVATE);
        if ("red".equals(init.getString("theme", ""))){  // 判断当前风格是什么
            setTheme(R.style.AppThemeRed);
        } else if ("blue".equals(init.getString("theme", ""))) {
            setTheme(R.style.AppThemeBlue);
        } else if ("yellow".equals(init.getString("theme", ""))) {
            setTheme(R.style.AppThemeYellow);
        } else if ("mr".equals(init.getString("theme", ""))) {
            setTheme(R.style.AppTheme);
        }
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        SharedPreferences.Editor editor = init.edit();
        switch (v.getId()) {
            case R.id.blue:
                editor.putString("theme", "blue");
                break;
            case R.id.red:
                editor.putString("theme", "red");
                break;
            case R.id.yellow:
                editor.putString("theme", "yellow");
                break;
            case R.id.mr:
                editor.putString("theme", "mr");
                break;
            default:
                break;
        }
        editor.commit();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}